package com.huitone.smspfm.interfacz.core.filewriter;

import java.util.Date;

import com.bin.common.utils.DateTimeUtils;
import com.bin.common.utils.StringUtils;

public class CuttingFileConfig {

	
	private int serial = 0;
	private int maxSerial = 99999999;
	private boolean useSerial = true;
	
	
	private String prefix = "send";
	private String suffix = ".xml";
	private String nameFlag = null;
	private String timePattern = "yyyyMMddHHmmssSSS";
	
	private CreateName interfaceName = null;
	
	private CuttingFileConfig() {}
	
	public CuttingFileConfig(String prefix, String suffix) {
		this.prefix = prefix;
		this.suffix = suffix;
	}
	
	public String getNameFlag() {
		return nameFlag;
	}

	public void setNameFlag(String nameFlag) {
		this.nameFlag = nameFlag;
	}

	public String getPrefix() {
		return prefix;
	}
	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}
	public String getSuffix() {
		return suffix;
	}
	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}
	
	public synchronized String getSerial() {
		
		if (serial >= maxSerial) {
			serial = 0;
		} else {
			serial ++;
		}
		
		return String.format("%08d", serial);
	}
	
	public int getMaxSerial() {
		return maxSerial;
	}
	public void setMaxSerial(int maxSerial) {
		this.maxSerial = maxSerial;
	}
	public boolean isUseSerial() {
		return useSerial;
	}
	public void setUseSerial(boolean useSerial) {
		this.useSerial = useSerial;
	}
	public String getTimePattern() {
		return timePattern;
	}
	public void setTimePattern(String timePattern) {
		this.timePattern = timePattern;
	}
	
	
	public CreateName getInterfaceName() {
		return interfaceName;
	}

	public void setInterfaceName(CreateName interfaceName) {
		this.interfaceName = interfaceName;
	}

	@Override
	public String toString() {
		
		StringBuffer buffer = new StringBuffer();
		buffer.append(StringUtils.toString(prefix));
		buffer.append(DateTimeUtils.dateToStr(new Date(), timePattern));
		if(nameFlag != null){
			buffer.append("_").append(StringUtils.toString(nameFlag));
		}
		if (useSerial) {
			buffer.append("_").append(getSerial());
		}
		buffer.append(StringUtils.toString(suffix));
		
		return  buffer.toString();
	}

	public String creaeName() {
		
		return this.toString();
		
	}
	
	public interface CreateName{
		String createFileName(String time);
	}

}
